﻿
#include "Test/LineTrace.h"


ALineTrace::ALineTrace()
{
	PrimaryActorTick.bCanEverTick = true;
}

void ALineTrace::BeginPlay()
{
	Super::BeginPlay();
	
}

void ALineTrace::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
}

void ALineTrace::LineTrace(const FVector& Start, const FVector& End, bool IgnoreSelf)
{

	FHitResult Hit;
	FCollisionQueryParams QueryParams;

	// 是否忽略自身
	if (IgnoreSelf) QueryParams.AddIgnoredActor(this);
	
	GetWorld()->LineTraceSingleByChannel(Hit, Start, End, ECollisionChannel::ECC_Visibility, QueryParams);
	if (Hit.bBlockingHit)
	{
		const FString ActorName = Hit.GetActor()->GetName();	
		GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Green, FString::Printf(TEXT("检测到射线命中：%s，位置：%s"), *ActorName , *Hit.Location.ToString()));


		FVector HitLocation = Hit.Location;
		HitLocation.Normalize();
		DrawDebugPoint(GetWorld(), Hit.Location - HitLocation, 10.f, FColor::Green, false, 5.0f);
	}
	
	DrawDebugLine(GetWorld(), Start, End, FColor::Red, false, 5.0f);
	
}

void ALineTrace::BoxTrace()
{
	// auto World = GetWorld();
	// World->OverlapMultiByChannel();
	// World->overlap

	
	
}

